package com.carpedil.sm.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;

// 1. 注明这是一个配置类
@Configuration
// 2. 开启注解扫描
@ComponentScan(basePackages = {"com.carpedil.sm"})
// 3. 开启mapper扫描
@MapperScan(basePackages = {"com.carpedil.sm.dao"})
// 4. 开启AOP
@EnableAspectJAutoProxy(proxyTargetClass = true)
// 5. 开启事务管理
@EnableTransactionManagement
/** @author Administrator */
public class ApplicationConfig {
  /**
   * 6. 配置dataSource
   *
   * @param dbProperties jdbc配置文件
   * @return DataSource对象
   */
  @Bean
  public DataSource dataSource(DBProperties dbProperties) {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName(dbProperties.getDriverClassName());
    dataSource.setUrl(dbProperties.getUrl());
    dataSource.setUsername(dbProperties.getUsername());
    dataSource.setPassword(dbProperties.getPassword());
    dataSource.setInitialSize(3);
    dataSource.setMaxActive(10);
    dataSource.setMinIdle(3);

    return dataSource;
  }

  /**
   * 配置Mybatis分页插件
   *
   * @return MybatisPlusInterceptor
   */
  @Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
  }
  /**
   * 7. 配置sqlSessionFactoryBean对象
   *
   * @param dataSource dataSource
   * @return SqlSessionFactoryBean
   */
  @Bean
  public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(
      DataSource dataSource, MybatisPlusInterceptor interceptor) {
    MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
    // 设置mybatis数据源
    factory.setDataSource(dataSource);
    // 设置mybatis全局配置
    factory.setConfigLocation(new ClassPathResource("mybatis-configuration.xml"));
    // 设置mapper位置
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
      factory.setMapperLocations(resolver.getResources("mapper/*.xml"));
    } catch (IOException e) {
      e.printStackTrace();
    }
    // 设置别名
    factory.setTypeAliasesPackage("com.carpeidl.sm.po");
    // 设置安装分页插件
    factory.setPlugins(interceptor);
    return factory;
  }

  /**
   * 8. 配置事务管理
   *
   * @param dataSource 数据源
   * @return 事务管理对象
   */
  @Bean
  public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
    DataSourceTransactionManager txManager = new DataSourceTransactionManager();
    txManager.setDataSource(dataSource);
    return txManager;
  }
}
